package it.tukano.swingdeco.controls;

import it.tukano.reflection.MethodExt;
import it.tukano.swingdeco.ExtUtilities;
import java.awt.Insets;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.SwingConstants;

/**
 * Extends a JButton with some utility methods
 * @author pgi
 */
public class JButtonExt extends JButton {

    private static final long serialVersionUID = 0;
    
    /**
     * Empty constructor
     */
    public JButtonExt() {}

    /**
     * Initializes this button with an icon
     * @param icon the icon of the button
     */
    public JButtonExt(Icon icon) {
        super(icon);
    }

    /**
     * Instance initializer
     */
    public JButtonExt(String label) {
        super(label);
    }
    
    public JButtonExt alignLabelLeft() {
        setHorizontalAlignment(SwingConstants.LEFT);
        return this;
    }

    /**
     * Enabled this button
     * @return this
     */
    public JButtonExt setEnabled() {
        setEnabled(true);
        return this;
    }
    
    public JButtonExt setUndecorated() {
        setMargin(new Insets(0,0,0,0));
        return this;
    }

    /**
     * Set the text of this button
     * @param label the text of the button
     * @return this
     */
    public JButtonExt withText(String label) {
        setText(label);
        return this;
    }
    
    /**
     * Sets the icon of this button and returns this
     * @param icon the icon to set
     * @return this
     */
    public JButtonExt withIcon(Icon icon) {
        setIcon(icon);
        return this;
    }

    public JButtonExt withTip(String tip) {
        setToolTipText(tip);
        return this;
    }

    /**
     * Scales the font of this component and returns this
     * @param factor the scale factor
     * @return this
     */
    public JButtonExt scaleFont(double factor) {
        ExtUtilities.scaleFont(this, factor);
        return this;
    }

    /**
     * Executes the given task when the user triggers the button
     * @param task the task to execute. Can be null.
     * @return this
     */
    public JButtonExt onActionPerformed(final Runnable task) {
        if(task != null) ExtUtilities.onActionPerformed(this, task);
        return this;
    }
    
    /** Shortcut for onAction(new MethodExt(ref, methodName)) */
    public JButtonExt onAction(final Object ref, final String methodName) {
        return onAction(new MethodExt(ref, methodName));
    }
    
    /**
     * Shortcut for onActionPerformed(runnable).
     * @param task the task to execute
     * @return this
     */
    public JButtonExt onAction(final Runnable task) {
        if(task != null) ExtUtilities.onActionPerformed(this, task);
        return this;
    }

    /**
     * Add an action listener to this button
     * @param task the action listener to add
     * @return this
     */
    public JButtonExt onActionPerformed(final ActionListener task) {
        addActionListener(task);
        return this;
    }

    /**
     * Disables this button
     * @return this
     */
    public JButtonExt setDisabled() {
        setEnabled(false);
        return this;
    }

    /**
     * Set the tool tip text of this button
     * @param bundleId the id of the resource bundle
     * @param bundleKey the key of the resource
     * @return this
     */
    public JButtonExt setTip(String bundleId, String bundleKey) {
        ExtUtilities.setToolTipText(this, bundleId, bundleKey);
        return this;
    }

    public JButtonExt withName(String string) {
        setName(string);
        return this;
    }
}
